package com.example.community.controller.interceptor;

import com.example.community.entity.User;
import com.example.community.service.DataService;
import com.example.community.util.HostHolder;
import com.example.community.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

@Component
public class DataInterceptor implements HandlerInterceptor {

    @Autowired
    private DataService dataService;

    @Autowired
    private HostHolder hostHolder;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //统计UV
        String ip = request.getRemoteHost();
        dataService.recordUV(ip);

        //统计DAU
        User user =hostHolder.getUser();
        if (user != null){
            dataService.recordDAU(user.getId());
        }
//        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
//        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
//        //统计当日的UV
//        int currentUV = dataService.calculateUV()
        return true;
    }

//    @Override
//    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
////        Date date = new Date();
////        SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
////        Calendar calendar = Calendar.getInstance();
//        long currentUV = dataService.calculateUV(new Date());
//        if ()
//        modelAndView.addObject("currentUV",currentUV);
//    }
}
